package com.orange.cepmanagerweb.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.orange.cepmanagerweb.form.PhoneForm;
import com.orange.cepmanagerweb.model.Phone;
import com.orange.cepmanagerweb.service.PhoneService;

@Controller
@RequestMapping (value="/phone")
public class PhoneController {

	private PhoneService phoneService;

	private PhoneForm phoneForm;
	
	
	
	/**
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/view.do")
	public @ResponseBody Map<String,? extends Object> view() throws Exception {

		try{

			List<Phone> Phones = phoneService.getPhoneList();

			return getMap(Phones);

		} catch (Exception e) {

			return getModelMapError("Error retrieving Phones from database.");
		}
	}
	
	/**
	 * @param data
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/create.do")
	public @ResponseBody Map<String,? extends Object> create(@RequestParam Object data) throws Exception {

		try{
			System.out.println(" Controller create.action *** Object Received : " + ((Phone)data).toString());
			
			List<Phone> Phones = phoneService.create(data);
			System.out.println(" Controller create.action *** Object Received : " + Phones.toString());
			return getMap(Phones);

		} catch (Exception e) {

			return getModelMapError("Error trying to create Phone.");
		}
	}
	
	@RequestMapping(value="/register.do")
	public @ResponseBody Map<String,? extends Object> register(@RequestParam Object data) throws Exception {

		try{
			System.out.println(" Controller create.action *** Object Received : " + ((Phone)data).toString());
			
			List<Phone> Phones = phoneService.create(data);
			System.out.println(" Controller create.action *** Object Received : " + Phones.toString());
			return getMap(Phones);

		} catch (Exception e) {

			return getModelMapError("Error trying to create Phone.");
		}
	}
	
	/**
	 * @param data
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/update.do")
	public @ResponseBody Map<String,? extends Object> update(@RequestParam Object data) throws Exception {
		try{

			List<Phone> Phones = phoneService.update(data);

			return getMap(Phones);

		} catch (Exception e) {

			return getModelMapError("Error trying to update Phone.");
		}
	}
	
	/**
	 * @param data
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/delete.do")
	public @ResponseBody Map<String,? extends Object> delete(@RequestParam Object data) throws Exception {
		
		try{

			phoneService.delete(data);

			Map<String,Object> modelMap = new HashMap<String,Object>(3);
			modelMap.put("success", true);

			return modelMap;

		} catch (Exception e) {

			return getModelMapError("Error trying to delete Phone.");
		}
	}
	
	/**
	 * Generates modelMap to return in the modelAndView
	 * @param Phones
	 * @return
	 */
	private Map<String,Object> getMap(List<Phone> Phones){
		
		Map<String,Object> modelMap = new HashMap<String,Object>(3);
		modelMap.put("total", Phones.size());
		modelMap.put("data", Phones);
		modelMap.put("success", true);
		
		return modelMap;
	}
	
	/**
	 * Generates modelMap to return in the modelAndView in case
	 * of exception
	 * @param msg message
	 * @return
	 */
	private Map<String,Object> getModelMapError(String msg){

		Map<String,Object> modelMap = new HashMap<String,Object>(2);
		modelMap.put("message", msg);
		modelMap.put("success", false);

		return modelMap;
	} 
	@RequestMapping("/searchPhones")
		 public ModelAndView searchPhones(@RequestParam(required= false, defaultValue="") String name)
		 {
	  ModelAndView mav = new ModelAndView("showPhones");
	  List<Phone> phones = phoneService.searchPhones(name.trim());
	  mav.addObject("SEARCH_PHONES_RESULTS_KEY", phones);
	  return mav;
	 }

	/**
	 * @param phoneForm
	 */
	@Autowired
	public PhoneController(PhoneForm phoneForm){
		this.phoneForm = phoneForm;
	}

	/**
	 * @param PhoneService
	 */
	@Autowired
	public void setPhoneService(PhoneService PhoneService) {
		this.phoneService = PhoneService;
	}
}
